﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ISIM
{
    public partial class FileTransferringDialog : Form
    {
        private string transferId;
        private SkypeKit.SktTransfer transferInfo;

        public FileTransferringDialog(string transferId, SkypeKit.SktTransfer transferInfo)
        {
            InitializeComponent();

            cancelTransferButton.Text = Global.LangManager.Translate("cancel");
            closeButton.Text = Global.LangManager.Translate("close");
            openDir.Text = Global.LangManager.Translate("directory");
            openFile.Text = Global.LangManager.Translate("open");

            this.transferId = transferId;
            this.transferInfo = transferInfo;

            this.Text = Global.Converter.GetRenamedContactName(transferInfo.P_PARTNER_HANDLE, transferInfo.P_PARTNER_DISPNAME);
            this.fileNameLabel.Text = transferInfo.P_FILENAME;
            this.fileSizeLabel.Text = Global.Converter.FormatSize(int.Parse(transferInfo.P_FILESIZE));
            this.transferStatusLabel.Text = transferInfo.P_STATUS.ToString();
        }

        public void UpdateTransferProgress()
        {
            if (this.cancelTransferButton.Enabled == false) return;

            int progress = (int)((float.Parse(transferInfo.P_BYTESTRANSFERRED) / float.Parse(transferInfo.P_FILESIZE)) * 100);

            if (progress > 100) progress = 100;
            else if (progress < 0) progress = 0;

            if (transferInfo.P_STATUS == SkypeKit.SktTransfer.STATUS.COMPLETED)
            {
                progress = 100;
                this.cancelTransferButton.Enabled = false;
                this.openFile.Enabled = true;
            }
            this.transferProgress.Value = progress;
            this.fileSizeLabel.Text = Global.Converter.FormatSize(int.Parse(transferInfo.P_BYTESTRANSFERRED)) + "/" + Global.Converter.FormatSize(int.Parse(transferInfo.P_FILESIZE)) + " (" + Global.Converter.FormatSize((int)transferInfo.P_BYTESPERSECOND) + "/s )";
            this.transferStatusLabel.Text = transferInfo.P_STATUS.ToString();
        }



        private void FileTransferringDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            Global.OpenForms.fileTransferringDialogList.Remove(transferId);
        }

        private void FileTransferringDialog_Load(object sender, EventArgs e)
        {
            Global.OpenForms.fileTransferringDialogList.Add(transferId, this);
        }

        private void cancelTransferButton_Click(object sender, EventArgs e)
        {
            transferInfo.Cancel();
            this.transferStatusLabel.Text = SkypeKit.SktTransfer.STATUS.CANCELLED.ToString();
            this.cancelTransferButton.Enabled = false;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void openDir_Click(object sender, EventArgs e)
        {
            string args = string.Format("/Select, {0}", transferInfo.P_FILEPATH);

            ProcessStartInfo pfi = new ProcessStartInfo("explorer.exe", args);
            Process.Start(pfi);
        }

        private void openFile_Click(object sender, EventArgs e)
        {
            Process.Start(transferInfo.P_FILEPATH);
        }
    }
}
